<?php

namespace App\Http\Controllers;

use App\Services\SDK\SDK;
use App\Services\SDK\SDKFactory;
use Illuminate\Http\Request;

class SDKController extends Controller
{
    protected $SDKFactory;

    /**
     * SDKController constructor.
     * @param SDKFactory $SDKFactory
     */
    public function __construct(SDKFactory $SDKFactory)
    {
        $this->SDKFactory = $SDKFactory;
    }

    /**
     * @param Request $request
     * @param $sdkName
     * @return array
     */
    public function verifyLogin(Request $request, $sdkName)
    {
        /**
         * @var $sdkHandler SDK
         */
        $sdkHandler = $this->SDKFactory->getSdk($sdkName);
        list($result, $data, $msg) = $sdkHandler->loginVerify($request);
        if ($result) {
            return  $this->ret(self::SUCCESS_CODE, ['user_id' => $data], $msg);
        }
        return $this->ret(self::FAILED_CODE, ['user_id' => $data], $msg);
    }

    /**
     * 支付回调
     * @param Request $request
     * @param $sdkName
     * @return array
     */
    public function payCallback(Request $request, $sdkName)
    {
        /**
         * @var $sdkHandler SDK
         */
        $sdkHandler = $this->SDKFactory->getSdk($sdkName);

        return $sdkHandler->payCallback($request);
    }

    /**
     * @param Request $request
     * @param $sdkName
     * @return mixed
     */
    public function testPay(Request $request, $sdkName)
    {
        /**
         * @var $sdkHandler SDK
         */
        $sdkHandler = $this->SDKFactory->getSdk($sdkName);

        if (method_exists($sdkHandler, 'testPay')) {
            return $sdkHandler->testPay($request);
        }

        return $this->ret(self::FAILED_CODE);
    }
}
